//
//  ComposeViewController.swift
//  weibo_swift
//
//  Created by dulinlong on 15/7/6.
//  Copyright © 2015年 sheji_56. All rights reserved.
//

import UIKit
import FFAutoLayout

class ComposeViewController: UIViewController {



    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.whiteColor()
        //准备导航栏
        prepareNavigation()
        
        //准备撰写的文本
        prepareTextView()
        
        // 1. 添加子控制器
        addChildViewController(emototiconViewController)

    }

    
    //MARK: - 点击事件
    func close(){
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    func sendMessage(){
        print(textView.emoticonText())
    }
    
    //toolbar上的选择图片
    func selectPicture(){
        
    }
    
    //toolbar上的输入表情
    private var isEmoticon = false
    func inputEmoticon(){
        print(__FUNCTION__)
        isEmoticon = !isEmoticon
        
        textView.resignFirstResponder()
        
        textView.inputView = isEmoticon ? emototiconViewController.view : nil
        textView.becomeFirstResponder()
    }
    
        //  表情键盘的controller
    private lazy var emototiconViewController: EmoticonViewController = EmoticonViewController { [unowned self] (emoticon) -> () in
        
        //textView中插入表情
        self.textView.insertEmoticon(emoticon)
        
    }

    
    
    //MARK: - 准备工作
    //准备navigation
    private func prepareNavigation(){
        //左侧
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
        
        //右侧
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: "sendMessage")
        
        //中间
        navigationItem.titleView = titleView
    }
    
    //准备撰写的文本
    private func prepareTextView(){
        view.addSubview(textView)
        
        textView.text = "分享微博..."
        textView.font = UIFont.systemFontOfSize(18)
        textView.ff_Fill(view)
        
        textView.becomeFirstResponder()
        
        // 设置垂直滚动
        textView.alwaysBounceVertical = true
        // 滚动关闭键盘
        textView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag

        
        //设置辅助视图
        textView.inputAccessoryView = toolBar
        
        
        //设置占位标签
        textView.addSubview(placeHolderLabel)
        placeHolderLabel.text = "分享微博..."
        placeHolderLabel.ff_AlignInner(ff_AlignType.TopLeft, referView: textView, size: nil , offset: CGPoint(x: 5, y: 8))
        
    
    }

    //MARK: - 懒加载数据
    //文本视图
    lazy var textView = UITextView()
    
    //提示label
    lazy var placeHolderLabel = UILabel(color: UIColor.lightGrayColor(), fontSize: 18)
    //navigation的自定义titleView
    lazy var titleView: UIView = {
        let v = UIView(frame: CGRect(x: 0, y: 0, width: 80, height: 40))
        
        let titleLabel = UILabel(color: UIColor.darkGrayColor(), fontSize: 15)
        let nameLabel = UILabel(color: UIColor(white: 0.5, alpha: 1.0), fontSize: 13)
        
        titleLabel.text = "发微博"
        nameLabel.text = sharedUserAccount?.name ?? ""
        
        v.addSubview(titleLabel)
        v.addSubview(nameLabel)
        
        titleLabel.sizeToFit()
        nameLabel.sizeToFit()
        titleLabel.ff_AlignInner(ff_AlignType.TopCenter, referView: v, size: nil)
        nameLabel.ff_AlignInner(ff_AlignType.BottomCenter, referView: v, size: nil)
        
        return v
    }()
    
    //textView的inputAccesseryView
    lazy var toolBar: UIToolbar = {
        let bar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 200, height: 44))
        
        bar.backgroundColor = UIColor(white: 0.8, alpha: 1.0)
        
        let itemSettings = [["imageName": "compose_toolbar_picture", "action": "selectPicture"],
            ["imageName": "compose_mentionbutton_background"],
            ["imageName": "compose_trendbutton_background"],
            ["imageName": "compose_emoticonbutton_background", "action": "inputEmoticon"],
            ["imageName": "compose_addbutton_background"]]
        
        var items = [UIBarButtonItem]()
        
        for item in itemSettings{
            items.append(UIBarButtonItem(imageName: item["imageName"]!, highlightedName: nil, target: self, actionName: item["action"]))
            
           items.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil))
        }
        items.removeLast()
        
        bar.items = items
        
        return bar
    }()

}
